<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>采购计划</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>
<body>
	<script type="text/javascript">
	var editIndex = undefined;
	//结束正在编辑的行
	function endEditing1() {
		if (editIndex == undefined) {
			return true
		}
		if ($('#lfq-tbmx2').datagrid('validateRow', editIndex)) {
			$('#lfq-tbmx2').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	//事件：单击单元格的事件
	function onClickCell1(index, field) {
		if (editIndex != index) {
			if (endEditing1()) {
				$('#lfq-tbmx2').datagrid('selectRow', index).datagrid('beginEdit',
						index);
				var ed = $('#lfq-tbmx2').datagrid('getEditor', {
					index : index,
					field : field
				});
				if (ed) {
					($(ed.target).data('textbox') ? $(ed.target).textbox(
							'textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function() {
					$('#lfq-tbmx2').datagrid('selectRow', editIndex);
				}, 0);
			}
		}
	}

	//结束编辑，同步数据
	function onEndEdit1(index, row) {
		var ed = $(this).datagrid('getEditor', {
			index : index,
			field : 'cpUnit'
		});
		row.cpUnit = $(ed.target).combobox('getText');
	}

	//添加行
	function append1() {
		if (endEditing1()) {
			$('#lfq-tbmx2').datagrid('appendRow', {
				'cpName' : [],
				'cpPrice' : [],
				'cpUnit' : [],
				'cgxqNumber' : [],
				'gysNo' : [],
				'cpApproval' : [],
				'cpType' : []
			});
			editIndex = $('#lfq-tbmx2').datagrid('getRows').length - 1;
			$('#lfq-tbmx2').datagrid('selectRow', editIndex).datagrid('beginEdit',
					editIndex);
		}
	}

	//删除行
	function removeit1() {
		if (editIndex == undefined) {
			return
		}
		$('#lfq-tbmx2').datagrid('cancelEdit', editIndex).datagrid('deleteRow',
				editIndex);
		editIndex = undefined;
	}

	//应用
	function accept1() {
		if (endEditing1()) {
			//$('#dg').datagrid('acceptChanges');
			endEditing();
		}
	}

	//撤消
	function reject1() {
		$('#lfq-tbmx2').datagrid('rejectChanges');
		editIndex = undefined;
	}

	//获取数据
	function getChanges1() {
		var rows = $('#lfq-tbmx2').datagrid('getChanges');
		alert(rows.length + ' rows are changed!');
	}

	//提交采购计划
	function savePlans1() {
		
		//获取所有行索引
		var rows = $("#lfq-tbmx2").datagrid("getRows");
		//将数据保存到数组中
		var arrs = [];
		var zz=0;
		var ygNo=$("#aa2").combogrid("getValue");
		var caigouren=$("#aa4").combogrid("getValue");
		if(ygNo.length==0){
			zz++;
		}
		if(caigouren.length==0){
			zz++;
		}
		for (var i = 0; i < rows.length; i++) {
			var index = rows[i];
			arrs.push(index.cpName);
			if(index.cpName.length==0){
				zz++;
			}
			arrs.push(index.cpPrice);
			if(index.cpPrice.length==0){
				zz++;
			}
			arrs.push(index.cpUnit);
			if(index.cpUnit.length==0){
				zz++;
			}
			arrs.push(index.cgxqNumber);
			if(index.cgxqNumber==null || index.cgxqNumber.length==0){
				zz++;
			}
			arrs.push(index.gysNo);
			if(index.gysNo==null || index.gysNo.length==0){
				zz++;
			}
			arrs.push(index.cpApproval);
			if(index.cpApproval.length==0){
				zz++;
			}
			arrs.push(index.cpType);
			if(index.cpType.length==0){
				zz++;
			}
		}
		console.log("arrs长度：" + arrs.length);
		//用form提交
		if(zz>0){
			$.messager.alert("提示", "您还有"+zz+"处药品信息没填写完整！");
		}else{
			if(rows.length>0){
				$("#lfq-purchasePlan-plan2").form('submit', {
					onSubmit : function(param) {
						param.arr = arrs;
					},
					success : function(data) {
						//var json = eval("(" + data + ")");
						$("#lfq-xitongpurchasePlan-win-add2").window("close");
						$("#lfq-tb-purchasePlan").datagrid("reload");
						$.messager.alert("提示", "成功添加采购计划！");
					}
				});
			}else{
				$.messager.alert("提示", "请先添加采购药品！");
			}
			
				
		}
	}
</script>
<script type="text/javascript">
    $(function(){
    	$("#lfq-tbmx2").datagrid({
			url : 'selectYuJingDrug'
		});
    	
    	$('#lfq-tbmx2').datagrid({
        	rowStyler: function(index,row){
        		if (row.cgxqNumber==null || row.cgxqNumber.length==0){
        			return 'background-color:#e6e6f2;'; 	
        		}else if(row.gysNo==null || row.gysNo.length==0){
        			return 'background-color:#e6e6f2;'; 
        		}
        	}
        });
    	
    });
</script>

	<div class="easyui-layout" data-options="fit:true">
		<form id="lfq-purchasePlan-plan2" action="PurchasePlanDoplan"
			method="post">
			<table width="100%"
				style="background: rgb(236, 243, 255); padding: 10px; border-radius: 5px;">
				<tr>
					<td><input class="easyui-textbox" readonly="readonly" id="aa1"
						value="${PurchasePlanId2}" name="hisPurchaseplan.cgjhNo"
						data-options="label:'采购计划ID', width:'30%'" />&nbsp;&nbsp;&nbsp;&nbsp;
						<select class="easyui-combogrid" id="aa2"
						name="hisPurchaseplan.ygNo"
						data-options="
										label:'制定人', 
										width:'30%', 
										panelWidth: '230',
										url:'ckguanliyuanAll',
										panelHeight:'auto',
										idField: 'ygNo',
										textField: 'ygName',
										columns:[[
										    {field:'ygNo',title:'编号',width:60},
							                {field:'ygName',title:'姓名',width:80},
							                {field:'ygPost',title:'职务',width:80}
							            ]]
										"></select>&nbsp;&nbsp;&nbsp;&nbsp;
						<select class="easyui-combogrid" id="aa4" name="caigouren"
						data-options="
										label:'采购人', 
										width:'30%', 
										panelWidth: '230',
										panelHeight: 'auto',
										url:'caigouAll',
										idField: 'ygNo',
										textField: 'ygName',
										columns:[[
										    {field:'ygNo',title:'编号',width:60},
							                {field:'ygName',title:'姓名',width:80},
							                {field:'ygPost',title:'职务',width:80}
							            ]]
										"></select>
					</td>
				</tr>
			</table>

			<div style="margin: 5px;">
				<div id="mm" class="easyui-tabs"
					style="width: 100%; margin-top: 10px;"
					data-options="plain:true,narrow:true">
					<div title="采购计划明细" style="padding: 10px;">
						<table id="lfq-tbmx2" class="easyui-datagrid"
							style="height: 250px"
							data-options="
											iconCls: 'icon-edit',
											singleSelect: true,
											rownumbers: true,
											toolbar: '#lfq-tb3',
											method: 'get',
											onClickCell: onClickCell1,
											onEndEdit: onEndEdit1,
											width: '100%'
										">
							<thead>
								<tr>
									<th data-options="field:'cpName',width:'21%',editor:{type:'textbox',options:{readonly:true}}">药物名称</th>
									<th
										data-options="field:'cpPrice',width:'10%',editor:{type:'numberbox',options:{precision:0,min:1}}">价格</th>
									<th
										data-options="field:'cpUnit',width:'10%',editor:{
													type:'combobox',
													options:{
														valueField:'label',
														textField:'value',
														panelHeight: 'auto',
														data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'},{label:'斤',value:'斤'}
														    ,{label:'包',value:'包'},{label:'件',value:'件'},{label:'个',value:'个'}
														    ,{label:'卷',value:'卷'}]
													}
												}">单位</th>
									<th
										data-options="field:'cgxqNumber',width:'10%',editor:{type:'numberbox',options:{precision:0,min:1}}">数量</th>
									<th
										data-options="field:'gysNo',width:'20%',editor:{
													type:'combobox',
													options:{
													    url:'selectSupplierAll',
														valueField: 'gysNo',
														textField: 'gysName',
														panelHeight: '100'
													}
												}">供应商</th>
									<th
										data-options="field:'cpApproval',width:'20%',align:'center',editor:{type:'textbox',options:{readonly:true}}">批文编号</th>
									<th
										data-options="field:'cpType',width:'10%',align:'center',editor:{
													type:'combobox',
													options:{
														valueField:'label',
														textField:'value',
														panelHeight: 'auto',
														data: [{label:'中药',value:'中药'},{label:'西药',value:'西药'}]
													}
												}">类型</th>
								</tr>
							</thead>
						</table>

						<div id="lfq-tb3" style="height: auto">
							<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-remove',plain:true"
								onclick="removeit1()">移除药品</a> <a href="javascript:void(0)"
								class="easyui-linkbutton"
								data-options="iconCls:'icon-ok',plain:true" onclick="accept1()">应用</a>
						</div>
					</div>
				</div>

				<div style="margin-top: 10px;">
					<a style="float: right;" class="easyui-linkbutton"
						data-options="iconCls:'icon-ok'" onclick="savePlans1()">提交采购计划</a>
				</div>
			</div>
		</form>
	</div>
</body>
</html>